/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.html;

import java.util.List;

import org.jxreport.core.Table;
import org.jxreport.core.views.table.TableView;
import org.w3c.dom.Element;

public class HTMLTableView extends TableView implements HTMLView{
	
	public HTMLTableView(Element tableElement, Table table) {
		super(tableElement, table);
	}

	@Override
	public String getJQueryCode() {
		return "$('#" + this.getId() + " table').dataTable();";
	}

	@Override
	public String getInnerDivCode() {
		//String html = "<div id='" + super.id + "'>";
		String html = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"display\" width=\"100%\">";
		//if(super.title != null) html += "<h1>" + super.title + "</h1>";
		
		html += this.getTHeadCode();
		html += this.getTBodyCode();
		
		html += "</table>";
		return html;
	}

	protected String getTHeadCode() {
		String html = "<thead>";
		
		for (String header: super.cols.keySet())
			html += "<th>" + header + "</th>";
		
		html+= "</thead>";
		return html;
	}
	
	protected String getTBodyCode() {
		String html = "<tbody>";
		
		for (int i = 0; i < this.maxSize; i++) {
			
			html += "<tr>";
			for (String header: super.cols.keySet())
			{
				List<String> values = super.cols.get(header);
				try{ html += "<td>" + values.get(i) + "</td>"; }
				catch(Exception e){ html += "<td></td>"; }
			}
			html += "</tr>";
		}
		
		html+= "</tbody>";
		return html;
	}
	
}
